package com.example.recur;

/**
 * @Date 2023-07-02
 * @Author Jonathan
 */
public class Factorial {
    public static void main(String[] args) {
        System.out.println(new Factorial().factorial(6));
        System.out.println(new Factorial().factorial(6, 1L));
    }

    public Long factorial(int n) {
        if (n == 1) {
            return 1L;
        }
        return n * factorial(n - 1);
    }

    /**
     * 尾递归
     * 时间复杂度O(n)
     * 空间复杂度O(n)
     */
    public Long factorial(int n, Long res) {
        if (n == 1) {
            return res;
        }
        return factorial(n - 1, n * res);
    }
}
